package com.example.mybaiduweishi.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.fragments.base.BaseFragmentbase;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.views.CircleTextImageView;
import com.example.mybaiduweishi.vo.Contacts;
import com.github.lzyzsd.randomcolor.RandomColor;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class ShowContactsFragment extends BaseFragmentbase {
    @BindView(R.id.show_contacts_fragment)
    RecyclerView recyclerView;

    private MyContactsAdapter adapter;

    @Override
    public int getXML() {
        return R.layout.show_all_fragment;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        //1、得到数据
        List<Contacts> list = MyUtil.getAllContacts();
        //2、适配器
        MyContactsAdapter adapter = new MyContactsAdapter(list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }


    class MyContactsAdapter extends RecyclerView.Adapter<MyContactsAdapter.MyViewHolder> {
        private List<Contacts> list;
        private Context context;

        public MyContactsAdapter(List<Contacts> list) {
            this.list = list;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_contacts, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            //绑定数据
            Contacts contacts = list.get(position);
            holder.ctiv.setText(contacts.getName().substring(1));
            //颜色随机
            RandomColor randomColor = new RandomColor();
            holder.ctiv.setFillColor(randomColor.randomColor());
            holder.tv_username.setText(contacts.getName());
            holder.tv_telphone.setText(contacts.getTelnumber());
            holder.tv_address.setText(contacts.getAddress());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        //组件实例化
        class MyViewHolder extends RecyclerView.ViewHolder {
            CircleTextImageView ctiv;
            TextView tv_username;
            TextView tv_address;
            TextView tv_telphone;
            CheckBox ck_choose;

            public MyViewHolder(View itemView) {
                super(itemView);
                ctiv = (CircleTextImageView) itemView.findViewById(R.id.ctiv);
                tv_username = (TextView) itemView.findViewById(R.id.tv_name);
                tv_address = (TextView) itemView.findViewById(R.id.tv_address);
                tv_telphone = (TextView) itemView.findViewById(R.id.tv_telphone);
                ck_choose = (CheckBox) itemView.findViewById(R.id.check_choose);
            }
        }
    }
}